/*
 * Copyright © Yolanda. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.transformpage.NoHttp.queue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created in Mar 27, 2016 8:31:08 PM.
 * 
 * @author Yolanda;
 */
public class YolandaQueue {

	private BlockingQueue<Request> blockingQueue;

	private TaskExecutor[] taskExecutors;

	private AtomicInteger atomicInteger = new AtomicInteger();

	public YolandaQueue(int poolSize) {
		// 如果Comparable#compareTo(Object)方法不做比较返回0，那么是无序的
		blockingQueue = new PriorityBlockingQueue<Request>();
		taskExecutors = new TaskExecutor[poolSize];
	}

	public void add(Request request) {
	//	request.setOrder(atomicInteger.incrementAndGet());
		blockingQueue.add(request);
	}

	public void start() {
		for (int i = 0; i < taskExecutors.length; i++) {
			//taskExecutors[i] = new TaskExecutor(blockingQueue, this);
			taskExecutors[i].start();
		}
	}

	public void stop() {
		for (TaskExecutor taskExecutor : taskExecutors) {
			taskExecutor.setRunning(false);
			taskExecutor.interrupt();
		}
	}

}
